package com.xinchao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinchao.core.constant.DeviceWarnConstant;
import com.xinchao.core.constant.PageConstant;
import com.xinchao.dao.DeviceWarnLogMapper;
import com.xinchao.model.vo.DeviceWarnLogVO;
import com.xinchao.service.DeviceWarnLogService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 终端报警日志
 *
 * @author linqiang
 * @date 2018-08-09 16:15
 */
@Service(value = "deviceWarnLogService")
public class DeviceWarnLogServiceImpl implements DeviceWarnLogService {

    @Autowired
    private DeviceWarnLogMapper deviceWarnLogMapper;

    @Override
    public Page<DeviceWarnLogVO> listByPage(String currentCity, Integer pageNum, Integer pageSize) {
        // 设置分页默认值
        pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
        pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceWarnLogVO> deviceWarnLogVOPage = deviceWarnLogMapper.listByPage(currentCity);
        if (null != deviceWarnLogVOPage && CollectionUtils.isNotEmpty(deviceWarnLogVOPage.getResult())) {
            List<DeviceWarnLogVO> result = deviceWarnLogVOPage.getResult();
            String content = "";
            for (DeviceWarnLogVO aResult : result) {
                content = "";
                if (StringUtils.isNotBlank(aResult.geteMail())) {
                    content = String.format(DeviceWarnConstant.EMAIL_TEMPLATE, aResult.geteMail());
                } else if (StringUtils.isNotBlank(aResult.getMobilePhone())) {
                    content = String.format(DeviceWarnConstant.PHONE_TEMPLATE, aResult.getMobilePhone());
                }
                aResult.setWarnContent(aResult.getWarnContent() + content);
            }
        }
        return deviceWarnLogVOPage;
    }
}
